%%%-------------------------------------------------------------------
%%% @doc  
%%% Copyright (C) 2007, Lucas Carlson
%%% File    : drproxy.erl
%%% Author  : Lucas Carlson (lucas@rufy.com)
%%% @end
%%%-------------------------------------------------------------------
-module(drproxy).

-behaviour(application).
%%--------------------------------------------------------------------
%% Include files
%%--------------------------------------------------------------------
-include("drproxy.hrl").

%%--------------------------------------------------------------------
%% External exports
%%--------------------------------------------------------------------
-export([
	 start/2,
	 shutdown/0,
	 stop/1,
	 status/0,
	 defaults/1,
	 config_file/0,
	 config/1
	 ]).

%%--------------------------------------------------------------------
%% Macros
%%--------------------------------------------------------------------

%%--------------------------------------------------------------------
%% Records
%%--------------------------------------------------------------------

%%====================================================================
%% External functions
%%====================================================================
%%--------------------------------------------------------------------
%% @doc The starting point for an erlang application.
%% @spec start(Type, StartArgs) -> {ok, Pid} | {ok, Pid, State} | {error, Reason}
%% @end
%%--------------------------------------------------------------------
start(Type, StartArgs) ->
    case dp_sup:start_link(StartArgs) of
	{ok, Pid} -> 
	    {ok, Pid};
	Error ->
	    Error
    end.

%%--------------------------------------------------------------------
%% @doc Called to shudown the drproxy application.
%% @spec shutdown() -> ok 
%% @end
%%--------------------------------------------------------------------
shutdown() ->
    application:stop(drproxy).

%%--------------------------------------------------------------------
%% @doc Called to get status information for drproxy
%% @spec status() -> ok 
%% @end
%%--------------------------------------------------------------------
status() ->
    dp_server:status().

defaults(Type) ->
	Defaults = [
		{webport, 30383},
		{docroot, "/usr/local/drproxy/docroot"},
		{server_concurrent_connections, 4096},
		{servers, [node()]},
		{server_first_port, 3010},
		{request_timeout, 60000},
		{debug, false},
		{auth, false},
		{frequency, 3.0}
		
	],
	case lists:keysearch(Type, 1, Defaults) of
		{value, {Type, Value}} ->
			Value;
		false ->
			exit({default_value, not_found})
	end.

config_file() ->
	case application:get_env(drproxy, config) of
		{ok, Config} ->
			Config;
		_ ->
			error_logger:error_msg("The config file has not been specified~n"),
			exit({config, missing_config_file})
	end.

config(Type) ->
	Dict = utils:config(config_file()),
	case catch dict:fetch(Type, Dict) of
		{'EXIT', _} ->
			defaults(Type);
		R ->
			R
	end.

%%====================================================================
%% Internal functions
%%====================================================================

%%--------------------------------------------------------------------
%% Called upon the termintion of an application.
%%--------------------------------------------------------------------
stop(State) ->
    ok.
